package cn.ysnk.clz.util;

import java.util.List;

public class PagerUtil
{
  private List elements;
  private int pageSize = 20;
  private int currentPageNumber = 1;
  private int totalElements = 0;
  private int totalSize = 0;
  private String orderByCol;
  private String orderByType;
  private static final String restrictNumCode = "onKeypress='if(event.keyCode <45||event.keyCode> 57)return false;' onpaste='return false;'";
  private static final int PAGE_NUM_SHOW_COUNT = 7;

  public List getElements()
  {
    return this.elements;
  }

  public void setElements(List elements)
  {
    this.elements = elements;
  }

  public int getCurrentPageNumber()
  {
    return this.currentPageNumber;
  }

  public void setCurrentPageNumber(int currentPageNumber)
  {
    this.currentPageNumber = currentPageNumber;
  }

  public int getPageSize()
  {
    return this.pageSize;
  }

  public void setPageSize(int pageSize)
  {
    this.pageSize = pageSize;
  }

  public int getTotalElements()
  {
    return this.totalElements;
  }

  public void setTotalElements(int totalElements)
  {
    this.totalElements = totalElements;
  }

  public boolean isFirstPage()
  {
    return (getCurrentPageNumber() == 1);
  }

  public boolean isLastPage()
  {
    return (getCurrentPageNumber() >= getLastPageNumber());
  }

  public boolean hasNextPage()
  {
    return (getLastPageNumber() > getCurrentPageNumber());
  }

  public boolean hasPreviousPage()
  {
    return (getCurrentPageNumber() > 1);
  }

  public int getLastPageNumber()
  {
    return getCountPage(this.totalElements, this.pageSize);
  }

  public String getOrderByCol()
  {
    return this.orderByCol;
  }

  public void setOrderByCol(String orderByCol)
  {
    this.orderByCol = orderByCol;
  }

  public String getOrderByType()
  {
    return this.orderByType;
  }

  public void setOrderByType(String orderByType)
  {
    this.orderByType = orderByType;
  }

  public String getHTML(String nameSpace)
  {
    int pageNumber = getCurrentPageNumber();
    int maxPageNumber = getLastPageNumber();
    int total = getTotalElements();
    int pageSize = getPageSize();
    StringBuffer sb = new StringBuffer();
    sb.append("共 " + maxPageNumber + " 页  " + total + "条记录  ");
    sb.append(pageSize + "条/页  ");
    sb.append("<span style=\"cursor:pointer\" onclick=\"return " + nameSpace + "go('0');\">首页</span>  ");
    if (pageNumber > 1)
      sb.append("<span style=\"cursor:pointer\" onclick=\"" + nameSpace + "go('" + (pageNumber - 1) + "')\">上一页</span>  ");

    if (pageNumber < maxPageNumber)
      sb.append("<span style=\"cursor:pointer\" onclick=\"" + nameSpace + "go('" + (pageNumber + 1) + "')\">下一页</span>  ");

    sb.append("<span style=\"cursor:pointer\" onclick=\"return " + nameSpace + "go('" + maxPageNumber + "');\">末页</span>  ");
    sb.append("跳转到");

    if (maxPageNumber < 11) {
      sb.append("<select name=\"pageNumber\" style=\"width:50px\">");
      for (int i = 1; i <= maxPageNumber; ++i)
        if (i == pageNumber)
          sb.append("<option value='" + i + "' selected>" + i + "</option>");
        else
          sb.append("<option value='" + i + "'>" + i + "</option>");


      sb.append("</select>");
    } else {
      sb.append("<input type=\"text\" name=\"pageNumber\" value=\"" + pageNumber + "\" size=\"5\" class=\"textbox\">");
    }
    sb.append("页");
    sb.append("<span style=\"cursor:pointer\" onclick=\"return " + nameSpace + "go();\">go</span>");
    return sb.toString();
  }

  public String toHTML(String nameSpace)
  {
    StringBuffer sb = new StringBuffer();
    try
    {
      sb.append(bulidForemostPageHtml(nameSpace));
      sb.append(bulidPreviousPageHtml(nameSpace));
      sb.append(bulidMiddleHtml(nameSpace));
      sb.append(buildDynamicHtml(nameSpace));
      sb.append(bulidNextPageHtml(nameSpace));
      sb.append(bulidFinalPageHtml(nameSpace));
      sb.append(buildNumInfoHtml());
    } catch (Exception e) {
      e.printStackTrace();
    }

    return sb.toString();
  }

  public String printOrderHTML(String judgeColumn)
  {
    StringBuffer sb = new StringBuffer();
    if ((judgeColumn != null) && (judgeColumn.trim().length() > 0) && 
      (judgeColumn.equals(getOrderByCol()))) {
      if ("asc".equalsIgnoreCase(getOrderByType()))
        sb.append("<img alt=\"升序\" src=\"/html/icons/upOrder.png\">");
      else {
        sb.append("<img alt=\"降序\" src=\"/html/icons/downOrder.png\">");
      }

    }

    return sb.toString();
  }

  private String bulidMiddleHtml(String nameSpace)
  {
    int i;
    StringBuffer sb = new StringBuffer();

    if (isFirstPage()) {
      int loopMax = (7 >= getLastPageNumber()) ? getLastPageNumber() : 7;
      for (i = 1; i <= loopMax; ++i)
        sb.append(bulidSpanByPageNum(nameSpace, Integer.valueOf(i), String.valueOf(i)));
    }
    else if (isLastPage()) {
      int stratPageNum = (getLastPageNumber() - 6 > 0) ? getLastPageNumber() - 6 : 1;
      for (i = stratPageNum; i <= getLastPageNumber(); ++i)
        sb.append(bulidSpanByPageNum(nameSpace, Integer.valueOf(i), String.valueOf(i)));

    }
    else if (getLastPageNumber() <= 7) {
      for (i = 1; i <= getLastPageNumber(); ++i)
        sb.append(bulidSpanByPageNum(nameSpace, Integer.valueOf(i), String.valueOf(i)));
    }
    else
    {
      int needAddLastNumCount = (3 - this.currentPageNumber - 1 <= 0) ? 
        0 : 3 - this.currentPageNumber - 1;
      int needAddStartNumCount = (3 - getLastPageNumber() - this.currentPageNumber <= 0) ? 
        0 : 3 - getLastPageNumber() - this.currentPageNumber;

      int startPageNum = (this.currentPageNumber - needAddStartNumCount - 3 > 0) ? 
        this.currentPageNumber - needAddStartNumCount - 3 : 1;
      int lastPageNum = (getLastPageNumber() > this.currentPageNumber + 3 + needAddLastNumCount) ? 
        this.currentPageNumber + 3 + needAddLastNumCount : getLastPageNumber();

      for (i = startPageNum; i <= this.currentPageNumber; ++i)
        sb.append(bulidSpanByPageNum(nameSpace, Integer.valueOf(i), String.valueOf(i)));

      for (i = this.currentPageNumber + 1; i <= lastPageNum; ++i) {
        sb.append(bulidSpanByPageNum(nameSpace, Integer.valueOf(i), String.valueOf(i)));
      }

    }

    return sb.toString();
  }

  private String buildDynamicHtml(String nameSpace)
  {
    StringBuffer sb = new StringBuffer();

    if (getLastPageNumber() > 7) {
      sb.append(buildInputPageNumHtml(nameSpace));
      sb.append("<a class='pageForward' onclick=\"return " + nameSpace + "go(document.getElementById('" + nameSpace + "pageNumber').value);\">&nbsp;跳转</a>");
    }
    return sb.toString();
  }

  private String buildSelectPageNumHtml(String nameSpace)
  {
    StringBuffer sb = new StringBuffer();

    sb.append("&nbsp;<select class='pageNumSelect' id=\"" + nameSpace + "pageNumber\">");
    for (int i = 1; i <= getLastPageNumber(); ++i)
      if (i == this.currentPageNumber)
        sb.append("<option class='font_style' value='" + i + "' style='background:#CFEBA5' selected><span class='font_style' >" + i + "</span></option>");
      else
        sb.append("<option value='" + i + "'><span class='font_style' >" + i + "</span></option>");


    sb.append("</select>&nbsp;");

    return sb.toString();
  }

  private String buildInputPageNumHtml(String nameSpace)
  {
    return "&nbsp;<input class='input_num font_style' type=\"text\" maxlength='4' onKeypress='if(event.keyCode <45||event.keyCode> 57)return false;' onpaste='return false;' id=\"" + nameSpace + "pageNumber\" value=\"" + this.currentPageNumber + "\" />";
  }

  private String bulidForemostPageHtml(String nameSpace)
  {
    if (isFirstPage()) {
      return bulidDisableSpanHtml("首页");
    }

    return bulidSpanByPageNum(nameSpace, Integer.valueOf(1), "首页", "span_unselected span_size_normal font_style");
  }

  private String bulidFinalPageHtml(String nameSpace)
  {
    if (isLastPage()) {
      return bulidDisableSpanHtml("&nbsp;&nbsp;末页");
    }

    return bulidSpanByPageNum(nameSpace, Integer.valueOf(getLastPageNumber()), "末页", "span_unselected span_size_normal font_style");
  }

  private String bulidNextPageHtml(String nameSpace)
  {
    if (isLastPage())
      return "";

    int reqPageNum = this.currentPageNumber + 1;
    return bulidSpanByPageNum(nameSpace, Integer.valueOf(reqPageNum), "下一页", "span_unselected span_size_long font_style");
  }

  private String bulidPreviousPageHtml(String nameSpace)
  {
    if (isFirstPage())
      return "";

    int reqPageNum = this.currentPageNumber - 1;

    return bulidSpanByPageNum(nameSpace, Integer.valueOf(reqPageNum), "上一页", "span_unselected span_size_long font_style");
  }

  private String bulidDisableSpanHtml(String displayName)
  {
    return "<span class='font_style'>" + displayName + "</span>&nbsp;";
  }

  private String bulidSpanByPageNum(String nameSpace, Integer pageNum, String displayName)
  {
    if (pageNum.intValue() == this.currentPageNumber)
      return bulidSpanByPageNum(nameSpace, pageNum, pageNum.toString(), "span_selected span_size_short font_style");

    return bulidSpanByPageNum(nameSpace, pageNum, pageNum.toString(), "span_unselected span_size_short font_style");
  }

  private String bulidSpanByPageNum(String nameSpace, Integer pageNum, String displayName, String classStyleNames)
  {
    if (pageNum.intValue() == this.currentPageNumber)
      return "<SPAN id='span_grid' class='" + classStyleNames + "'>" + displayName + "</SPAN>";

    return "<SPAN id='span_grid' pageNum='"+pageNum+"' class=' "+nameSpace+" " + classStyleNames + "' onclick=" + nameSpace + "go('" + pageNum + "')>" + displayName + "</SPAN>";
  }

  private String buildNumInfoHtml() {
    return "<span class='font_style'>&nbsp;&nbsp;共 <span class='span_page_num'> " + getLastPageNumber() + " </span>页 <span class='span_page_num'> " + getTotalElements() + " </span>条记录</span>";
  }
  
  private int getCountPage(int totalElements, int pageSize)
  {
    return ((totalElements - 1) / pageSize + 1);
  }
}